package com.yoomiito.app.present;

import com.yoomiito.app.base.AppConfig;
import com.yoomiito.app.base.BaseActPresenter;
import com.yoomiito.app.model.gift.GiftGoodsList;
import com.yoomiito.app.model.gift.GiftType;
import com.yoomiito.app.model.order.ShopCarList;
import com.yoomiito.app.net.YmApi;
import com.yoomiito.app.ui.YouPinGoodsActivity;
import com.yoomiito.app.utils.NetworkUtils;

import java.util.List;

import cn.droidlover.xdroidmvp.net.ApiException;
import cn.droidlover.xdroidmvp.net.ApiSubscriber;
import cn.droidlover.xdroidmvp.net.XApi;

public class YouPinGoodsPresenter extends BaseActPresenter<YouPinGoodsActivity> {

    public YouPinGoodsPresenter(AppConfig appConfig) {
        super(appConfig);
    }

    public void getVipGiftGoods(String sort, int page, long type,int goodsType){
        if (NetworkUtils.checkNetPermission(getV())){
            getV().finishRefresh();
            return;
        }
        YmApi.getYmService()
                .getVipGiftGoods(sort,page,type,goodsType,"")
                .compose(getV().bindToLifecycle())
                .compose(XApi.getApiTransformerData())
                .compose(XApi.getScheduler())
                .subscribe(new ApiSubscriber<GiftGoodsList>(handleException()) {
                    @Override
                    public void onSuccess(GiftGoodsList giftGoodsList) {
                        if (hasV()){
                            getV().showGift(giftGoodsList);
                            getV().dismissLoading();
                        }
                    }

                    @Override
                    public void onFail(ApiException error) {
                        if (hasV()){
                            getV().dismissLoading();
                        }
                    }
                });
    }

    public void getGiftType(int id){
        if (NetworkUtils.checkNetPermission(getV())){
            return;
        }
        YmApi.getYmService()
                .getGiftType(id)
                .compose(getV().bindToLifecycle())
                .compose(XApi.getApiTransformerData())
                .compose(XApi.getScheduler())
                .subscribe(new ApiSubscriber<List<GiftType>>(handleException()) {
                    @Override
                    public void onSuccess(List<GiftType> giftTypes) {
                        if (hasV()){
                            getV().setType(giftTypes);
                        }
                    }

                    @Override
                    public void onFail(ApiException error) {

                    }
                });
    }

    public void getList(int page){
        YmApi.getYmService()
                .showCarList(page,10)
                .compose(getV().bindToLifecycle())
                .compose(XApi.getApiTransformerData())
                .compose(XApi.getScheduler())
                .subscribe(new ApiSubscriber<ShopCarList>(handleException()) {
                    @Override
                    public void onSuccess(ShopCarList shopCarList) {
                        if (hasV()){
                            getV().dismissLoading();
                            if (shopCarList==null||shopCarList.getData()==null||shopCarList.getData().size()==0){
                                getV().showNewFlag(false);
                            }else {
                                getV().showNewFlag(true);
                            }
                        }
                    }

                    @Override
                    public void onFail(ApiException error) {
                        if (hasV()){
                            getV().dismissLoading();
                        }
                    }
                });
    }


}
